package org.ezplatform.office.rd.archives.web.rest;

import org.ezplatform.core.service.BaseService;
import org.ezplatform.core.web.controller.BaseController;
import org.ezplatform.office.approve.utils.ApproveValidateUtils;
import org.ezplatform.office.approve.web.rest.PendingTaskWebApiController;
import org.ezplatform.office.rd.archives.service.TySdagService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 省档案馆归档
 * 20230919guoxf
 */
@RestController
@RequestMapping(value = "/api/w/rd/sdagArchiveNew")
public class TySdagArchiveController extends BaseController {
    public static final Logger LOG = LoggerFactory.getLogger(TySdagArchiveController.class);
    @Autowired
    private TySdagService sdagService;



    @Override
    protected BaseService getEntityService() {
        return null;
    }

    //向档案系统推送zip
    @RequestMapping(value = "/pushFileToFtp", method = RequestMethod.GET)
    @ResponseBody
    public String pushFileToFtp(HttpServletRequest request){
        LOG.error("========pushFileToFtp接口开始执行========");
        //非法参数校验
        LOG.error("开始进行参数校验");
        ApproveValidateUtils.validateRequestParmas(request);
        LOG.error("参数校验通过");
        
        String id=request.getParameter("id");
        String type=request.getParameter("type");
        String pushUserName=request.getParameter("user");
        String formKey=request.getParameter("formKey");//表单编码
        LOG.error("获取请求参数: id={}, type={}, pushUserName={}, formKey={}", id, type, pushUserName, formKey);
        
        String isOK="true";
        try {
            LOG.error("开始检查是否为退回数据");
            boolean back=sdagService.isBack(id,type);
            LOG.error("退回检查结果: back={}", back);
            
            if(back){//被退回的走重新推送接口
                LOG.error("数据已被退回，执行重新推送逻辑");
                sdagService.rePush(id,pushUserName,formKey);
                LOG.error("重新推送执行完成");
            }else{
                LOG.error("数据未被退回，执行正常推送逻辑");
                if("send".equals(type)){
                    LOG.error("执行发文归档推送");
                    sdagService.createSendZip(id,pushUserName,formKey);
                    LOG.error("发文归档推送完成");
                }else{
                    LOG.error("执行收文归档推送");
                    sdagService.createReceiveZip(id,pushUserName,formKey);
                    LOG.error("收文归档推送完成");
                }
            }
            LOG.error("pushFileToFtp接口执行成功");
        } catch (Exception e) {
            isOK="false";
            LOG.error("pushFileToFtp接口执行异常: ", e);
            e.printStackTrace();
        }
        LOG.error("========pushFileToFtp接口执行结束，返回结果: {}========", isOK);
        return isOK;
    }
    //批量向档案系统推送zip
    @RequestMapping(value = "/batchPushFileToFtp", method = RequestMethod.GET)
    @ResponseBody
    public String batchPushFileToFtp(HttpServletRequest request){
        LOG.error("========batchPushFileToFtp批量推送接口开始执行========");
        //非法参数校验
        LOG.error("开始进行参数校验");
        ApproveValidateUtils.validateRequestParmas(request);
        LOG.error("参数校验通过");
        
        String ids=request.getParameter("ids");
        String type=request.getParameter("type");
        String pushUserName=request.getParameter("user");
        String formKeys=request.getParameter("formKeys");
        LOG.error("获取批量推送参数: ids={}, type={}, pushUserName={}, formKeys={}", ids, type, pushUserName, formKeys);
        
        int s=0;
        int f=0;
        int count=0;
        String titles="";
        try {
            if(ids!=null && !"".equals(ids)){
                count=ids.split(",").length;
                LOG.error("批量推送数据总数: {}", count);
                
                LOG.error("开始检查批量数据是否已推送");
                boolean isOK=sdagService.isPush(ids,type);
                LOG.error("批量推送检查结果: isOK={}", isOK);
                
                if(!isOK){
                    LOG.error("数据未推送，开始执行批量推送逻辑");
                    for(int i=0;i<count;i++){
                        String id=ids.split(",")[i];
                        String lcbm="";
                        LOG.error("处理第{}条数据，id={}", (i+1), id);
                        
                        LOG.error("检查数据id={}是否为退回数据", id);
                        boolean back=sdagService.isBack(id,type);
                        LOG.error("数据id={}退回检查结果: back={}", id, back);
                        
                        if(back){//被退回的走重新推送接口
                            LOG.error("数据id={}已被退回，执行重新推送", id);
                            sdagService.rePush(id,pushUserName,lcbm);
                            LOG.error("数据id={}重新推送完成", id);
                        }else{//正常推送走这
                            LOG.error("数据id={}未被退回，执行正常推送", id);
                            if("send".equals(type)){
                                LOG.error("数据id={}执行发文归档推送", id);
                                sdagService.createSendZip(id,pushUserName,lcbm);
                                LOG.error("数据id={}发文归档推送完成", id);
                            }else{
                                LOG.error("数据id={}执行收文归档推送", id);
                                sdagService.createReceiveZip(id,pushUserName,lcbm);
                                LOG.error("数据id={}收文归档推送完成", id);
                            }
                        }
                    }
                    LOG.error("批量推送处理完成");
                }else{
                    titles="包含已推送成功数据，本次推送失败。";
                    LOG.error("批量推送失败: {}", titles);
                    return titles;

                    /**
                     for(int i=0;i<ids.split(",").length;i++){
                        String title=sdagService.getPushStatus(ids.split(",")[i],type);
                        if("".equals(title)){
                            s++;
                            String id=ids.split(",")[i];
                            if("send".equals(type)){
                                sdagService.createSendZip(id);
                            }else{
                                sdagService.createReceiveZip(id);
                            }
                        }else{
                            titles+=title+",";
                            f++;
                        }
                     }
                     */
                }
            }else{
                LOG.warn("批量推送参数ids为空");
            }
        } catch (Exception e) {
            LOG.error("batchPushFileToFtp批量推送接口执行异常: ", e);
            e.printStackTrace();
        }
        titles="推送成功"+count+"条数据，推送失败0条数据，失败原因为该数据已经推送成功，不支持二次推送。";
        LOG.error("========batchPushFileToFtp批量推送接口执行结束，返回结果: {}========", titles);
        return titles;
    }

    @RequestMapping(value = "/repushFileToFtp", method = RequestMethod.GET)
    @ResponseBody
    public String repushFileToFtp(HttpServletRequest request){
        LOG.error("========repushFileToFtp重新推送接口开始执行========");
        //非法参数校验
        LOG.error("开始进行参数校验");
        ApproveValidateUtils.validateRequestParmas(request);
        LOG.error("参数校验通过");
        
        String id=request.getParameter("id");
        String pushUserName=request.getParameter("user");
        String lcbm=request.getParameter("lcbm");
        LOG.error("获取重新推送参数: id={}, pushUserName={}, lcbm={}", id, pushUserName, lcbm);
        
        try {
            LOG.error("开始执行重新推送业务逻辑");
            sdagService.rePush(id,pushUserName,lcbm);
            LOG.error("重新推送业务逻辑执行成功");
            return "重新推送成功";
        } catch (Exception e) {
            LOG.error("repushFileToFtp重新推送接口执行异常", e);
            return "重新推送失败："+e.getMessage();
        } finally {
            LOG.error("========repushFileToFtp重新推送接口执行结束========");
        }
    }

    /**
     * 查看推送详情
     * @param request
     * @return
     */
    @RequestMapping(value = "/getDetail", method = RequestMethod.GET)
    @ResponseBody
    public String getDetail(HttpServletRequest request){
        LOG.error("========getDetail查看推送详情接口开始执行========");
        //非法参数校验
        LOG.error("开始进行参数校验");
        ApproveValidateUtils.validateRequestParmas(request);
        LOG.error("参数校验通过");
        
        String id=request.getParameter("id");
        String type=request.getParameter("type");
        LOG.error("获取查看详情参数: id={}, type={}", id, type);
        
        String tip="";
        try {
            LOG.error("开始获取推送详情");
            tip=sdagService.getDetail(id,type);
            LOG.error("推送详情获取完成，详情长度: {}", tip != null ? tip.length() : 0);
        } catch (Exception e) {
            tip="获取详情失败，请联系管理员！";
            LOG.error("getDetail查看推送详情接口执行异常: ", e);
            e.printStackTrace();
        }
        LOG.error("========getDetail查看推送详情接口执行结束========");
        return tip;
    }

    /**
     * 查看退回详情
     * @param request
     * @return
     */
    @RequestMapping(value = "/getBackReason", method = RequestMethod.GET)
    @ResponseBody
    public String getBackReason(HttpServletRequest request){
        LOG.error("========getBackReason查看退回详情接口开始执行========");
        //非法参数校验
        LOG.error("开始进行参数校验");
        ApproveValidateUtils.validateRequestParmas(request);
        LOG.error("参数校验通过");
        
        String id=request.getParameter("id");
        LOG.error("获取查看退回原因参数: id={}", id);
        
        String tip="";
        try {
            LOG.error("开始获取退回原因");
            tip=sdagService.getBackReason(id);
            LOG.error("退回原因获取完成，原因长度: {}", tip != null ? tip.length() : 0);
        } catch (Exception e) {
            tip="获取退回原因失败，请联系管理员！";
            LOG.error("getBackReason查看退回详情接口执行异常: ", e);
            e.printStackTrace();
        }
        LOG.error("========getBackReason查看退回详情接口执行结束========");
        return tip;
    }

}
